12. Cost Exercise
The Cost of Action
Before we get into incorporating a map into our planning solution, we'll delve a little deeper into the notion of cost. It's relatively intuitive to think of diagonal actions as having a higher cost than lateral or vertical motions between grid cells because the distance travelled is slightly longer. However, actions might have associated costs for other reasons as well.
Here, we'll expand the functionality of our Action()
class to include the cost of each action using the following modifications:
from enum import Enum
class Action(Enum):
# Assign the cost of each action as the third element in the tuple
LEFT = (0, -1, 1)
RIGHT = (0, 1, 1)
UP = (-1, 0, 1)
DOWN = (1, 0, 1)
def __str__(self):
if self == self.LEFT:
return '<'
elif self == self.RIGHT:
return '>'
elif self == self.UP:
return '^'
elif self == self.DOWN:
return 'v'
# Assign a new property that returns the cost of an action
@property
def cost(self):
return self.value[2]
# Assign a property that returns the action itself
@property
def delta(self):
return (self.value[0], self.value[1])
Here we've added a third element each tuple defining actions, which is the cost of each action, and in this case is set to a value of one for all actions. We've also defined some new properties of the Action()
class that return the cost and delta (the action itself). With this you have everything you need to perform uniform cost search!
Cost Exercise
In this exercise you'll assign a cost to each action and you can even expand your action set to include diagonal motions if you like!
Good luck! And for a peek at our solution scroll down to the link at the bottom of the notebook.
Workspace
This section contains either a workspace (it can be a Jupyter Notebook workspace or an online code editor work space, etc.) and it cannot be automatically downloaded to be generated here. Please access the classroom with your account and manually download the workspace to your local machine. Note that for some courses, Udacity upload the workspace files onto https://github.com/udacity, so you may be able to download them there.
Workspace Information:
- Default file path:
- Workspace type: jupyter
- Opened files (when workspace is loaded): n/a